<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>蔬果鲜生 - 登录</title>
    <style>
        :root {
            --primary-green: #4CAF50;
            --primary-red: #FF5252;
            --light-green: #e8f5e9;
            --light-red: #ffcdd2;
        }

        body {
            margin: 0;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background: linear-gradient(135deg, var(--light-green), var(--light-red));
            font-family: 'Microsoft YaHei', sans-serif;
        }

        .bg-pattern {
            position: fixed;
            width: 100%;
            height: 100%;
            background: url('../static/images/file-695959843156421.png') repeat;
            opacity: 0.15;
            z-index: 0;
        }

        .login-container {
            position: relative;
            z-index: 1;
            background: rgba(255, 255, 255, 0.95);
            padding: 2.5rem;
            border-radius: 20px;
            box-shadow: 0 15px 40px rgba(0, 0, 0, 0.1);
            width: 90%;
            max-width: 420px;
            backdrop-filter: blur(10px);
        }

        .logo-header {
            text-align: center;
            margin-bottom: 2rem;
        }

        .logo-header img {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            margin-bottom: 1rem;
            box-shadow: 0 5px 15px rgba(76, 175, 80, 0.3);
        }

        .form-group {
            margin-bottom: 1.8rem;
        }

        label {
            display: block;
            margin-bottom: 0.6rem;
            color: var(--primary-green);
            font-weight: 500;
            font-size: 1.1rem;
        }

        input {
            width: 100%;
            padding: 13px 18px;
            border: 2px solid #eee;
            border-radius: 10px;
            font-size: 1rem;
            transition: all 0.3s ease;
        }

        input:focus {
            outline: none;
            border-color: var(--primary-green);
            box-shadow: 0 0 10px rgba(76, 175, 80, 0.2);
        }

        .login-btn {
            width: 100%;
            padding: 15px;
            background: linear-gradient(to right, var(--primary-green), var(--primary-red));
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 1.1rem;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .login-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 25px rgba(76, 175, 80, 0.3);
        }

        .auth-links {
            display: flex;
            justify-content: space-between;
            margin-top: 1.5rem;
            font-size: 0.95rem;
        }

        .auth-links a {
            color: #666;
            text-decoration: none;
            transition: color 0.3s;
        }

        .auth-links a:hover {
            color: var(--primary-green);
        }

        @media (max-width: 480px) {
            .login-container {
                margin: 1rem;
                padding: 1.8rem;
            }
        }
    </style>
</head>
<body>
<div class="bg-pattern"></div>
<div class="login-container">
    <div class="logo-header">
        <img src="../static/images/file-695959710016261.png" alt="蔬果鲜生Logo">
        <h1 style="color: var(--primary-green);">蔬果鲜生</h1>
    </div>

    <form action="sys/user/login" th:action="@{sys/user/login}" th:method="post">
        <div class="form-group">
            <label for="username">用户名：</label>
            <input type="text" id="username" name="username" placeholder="请输入用户名" required>
        </div>

        <div class="form-group">
            <label for="password">密码：</label>
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
        </div>

        <button type="submit" class="login-btn">立即登录</button>
    </form>

    <div class="auth-links">
        <a href="/register">新用户注册</a>
        <a href="/">返回首页</a>
        <a href="/reset-password">忘记密码？</a>
    </div>
</div>
</body>
</html>
<script>
    // 获取表单元素
    const loginForm = document.querySelector('form');
    const usernameInput = document.getElementById('username');
    const passwordInput = document.getElementById('password');
    // const rememberCheckbox = document.getElementById('remember');

    // 监听表单提交事件
    loginForm.addEventListener('submit', async function(e) {
        e.preventDefault(); // 阻止表单默认提交行为

        // 获取用户输入的数据
        const loginData = {
            username: usernameInput.value,
            password: passwordInput.value,
            // remember: rememberCheckbox.checked
        };

        try {
            // 调用后端登录接口
            const response = await fetch('sys/user/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(loginData)
            });
            // 检查响应状态
            if (response.ok) {
                const result = await response.json();
                console.log('响应数据：',result)
                // 获取后端返回的字段
                const code = result.code;
                const message = result.message;
                const data = result.data;

                console.log('响应 code:', code);
                console.log('响应 message:', message);
                console.log('响应 data:', data);
                // 登录成功处理
                if (result.code===0) {
                    // alert('登录成功！');
                    // 可以跳转到其他页面
                    window.location.href = '/info';
                } else {
                    alert('登录失败：' + result.message);
                }
            } else {
                alert('网络错误，请稍后重试');
            }
        } catch (error) {
            console.error('登录请求失败：', error);
            alert('登录失败，请检查网络连接');
        }
    });

    // 可选：添加输入验证
    function validateForm() {
        if (!usernameInput.value.trim()) {
            alert('请输入用户名');
            return false;
        }
        if (!passwordInput.value.trim()) {
            alert('请输入密码');
            return false;
        }
        return true;
    }

    // 在提交前验证表单
    loginForm.addEventListener('submit', function(e) {
        if (!validateForm()) {
            e.preventDefault();
        }
    });
</script>
